package com.banciyuan.bcywebview.biz.zanlist;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.utils.currency.CurrencyUtil;
import com.banciyuan.bcywebview.utils.universalimageloader.core.ImageLoader;
import com.banciyuan.bcywebview.utils.universalimageloader.utils.ImageOptions;

import java.util.List;

import de.greenrobot.daoexample.model.DetailGoods;


public class GoodsZanAdapter extends BaseAdapter {
    private Context context;
    private List<DetailGoods> data;
    private LayoutInflater mLayoutInflater;

    public GoodsZanAdapter(Context context, List<DetailGoods> data) {
        this.context = context;
        mLayoutInflater = LayoutInflater.from(context);
        this.data = data;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        DetailGoods detailGoods = data.get(position);
        ViewHolder viewHolder;
        if (null == convertView) {
            convertView = mLayoutInflater.inflate(R.layout.goods_zan_item, parent, false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        ImageLoader.getInstance().displayImage(detailGoods.getCover(), viewHolder.ivGoodsPic, ImageOptions.options);
        viewHolder.tvGoodsName.setText(detailGoods.getTitle());
        viewHolder.tvGoodsPrice.setText(String.format(context.getString(R.string.price_format), CurrencyUtil.getMoney(detailGoods.getBasic_price())));
        return convertView;
    }


    public void setData(List<DetailGoods> data) {
        this.data = data;
    }

    private static class ViewHolder {
        ImageView ivGoodsPic;
        TextView tvGoodsName;
        TextView tvGoodsPrice;

        public ViewHolder(View convertView) {
            ivGoodsPic = (ImageView) convertView.findViewById(R.id.iv_goods_pic);
            tvGoodsName = (TextView) convertView.findViewById(R.id.tv_goods_name);
            tvGoodsPrice = (TextView) convertView.findViewById(R.id.tv_goods_price);
        }
    }
}
